#include <head.h>
void sys_error(const char *e)
{
	perror(e);
	exit(1);
}
int Socket(int domain,int type,int protocol)
{
	int r = socket(domain,type,protocol);
	if(r ==-1){
		sys_error("socket error");
	}
	return r;
}
int Connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
{
	int r = connect(sockfd,addr,addrlen);
	if(r ==-1){
		sys_error("connect error");
	}
	return r;

}

int Bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
{
	int r = bind(sockfd,addr,addrlen);
	if(r == -1){
		sys_error("bind error");
	}
	return r;
}
int Listen(int sockfd, int backlog)
{
	int r = listen(sockfd,backlog);
	if(r == -1){
		sys_error("listen   error");
	}
	return r;
}


int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
	int r;
again:
	r = accept(sockfd,addr,addrlen);
	if(r == -1){
		if(errno == EINTR){
			goto again;
		}
		else{
			sys_error("accept error");
		}
	}
	return r;

}
ssize_t Read(int fd, void *buf, size_t count)
{
	ssize_t r;
again:
	r = read(fd,buf,count);
	if(r == -1){
		if(errno == EAGAIN ||errno == EWOULDBLOCK){
			goto again;
		}
		else if(errno== EINTR){
			goto again;
		}
		else{
			sys_error("read error");
		}
	}
	return r;
}
ssize_t Write(int fd, const void *buf, size_t count)
{
	ssize_t r;
again:
	r= write(fd,buf,count);
	if(r == -1){
		if(errno == EAGAIN ||errno == EWOULDBLOCK){
			goto again;
		}
		else if(errno== EINTR){
			goto again;
		}
		else{
			sys_error("write error");
		}
	}
	return r;


}
int Close(int fd)
{
	int r =close(fd);
	if(r == -1){
		sys_error("close error");
	}
	return r;
}
